define(function(require,exports,module) {
    var myPub = require('./public');
    // var lpkPay = require('./submit_lpkPay');
    var $orderForm = $('#orderForm');           // 需要支付的表单
    var $canBuy = $('#canBuy');  // 判断是否可以购买积分商品
    var $isJifen = $('#isJifen');  //判断是否是积分商品
    var $submitBtn = $(".js_btnPay");           // 提交支付按钮
    var oFullGroup = $('.js_fullGroup');                        // 拼单已满提示弹窗
    var oCashMain = $('.js_cashMain');
    var oShadows = $('.js_shadowlpk');
    var kcuid =$("#kcuid").val();//为爱狂吃活动？？？
    var tempOrderSn;
    var tempOrderType;
    var tempPaymentId;

    //如果是积分商品且不满足购买条件，对提交按钮置灰且不可点击
    /*if($isJifen.val() && $canBuy.val() == 'false'){
        //将按钮置灰
        $submitBtn.find('button').removeClass('btn_Bgred').addClass('btn_Bggray');
    }*/


    function submitPay(payUrl,payData) {
        /*if($isJifen.val() && $canBuy.val() == 'false'){
            //积分商品不满足条件，按钮不可点击
            return;
        }*/
        G_JSON_POST(payUrl,payData,function(data){
            tempOrderSn = data.one_order_sn;
            tempOrderType = data.order_type;
            tempPaymentId = data.payment_id;
            if("PAYFINISH" == data.return_code){
                paySuccess();
                return;
            }
            if("SUCCESS" != data.return_code){
                if ("FULL_FAIL" == data.return_code){
                    $("#groupBuyRecordId").val("");
                    autoDefault = 0;
                    oFullGroup.show();
                }else {
                    myPub.TipLayer(data.return_msg);
                }
                if (data.resultObject != null){
                    $('input[name="token"]').val(data.resultObject);
                }
                return;
            }
            if("ALIPAY" == data.pay_type){
                if(submitAliPay){
                    submitAliPay(data.return_from_text);
                }else{
                    forbiddenPayType("ALIPAY");
                }
            }else if("WEIXIN" == data.pay_type){
                if(submitWeixinPay){
                    submitWeixinPay(data.timeStamp,data.nonceStr,data.package,data.signType,data.paySign,paySuccess,payCancel,payFail);
                }else{
                    forbiddenPayType("WEIXIN");
                }
            }else if("GLOBAL_ALIPAY" == data.pay_type){
                submitAliPay(data.return_from_text);
            }else if("WEIXIN_HIPO" == data.pay_type){
                console.log("这里可以发起跳转请求--请求转发到hiPoPay的授权接口");
                //商户号和币种  这里直接写死。后面调试完毕后，在统一后台返回参数
                var sessionUid = myPub.getCookie('token');
                console.log("sessionUid为："+ sessionUid);
                window.location.href = 'https://api.wisecashier.com/mp_pay/oauth?merchant_no='+ data.merchantNo +'&currency='+ data.currency +'&sessionUid='+ sessionUid + '&sysTradeNo='+ data.payment_order_sn;

                // if(submitHiPoPay){
                //     submitHiPoPay(data.merchantNo,data.amount,data.openid,data.currency,data.productInfo,data.notifyUrl,paySuccess,payCancel,payFail);
                // }else{
                //     forbiddenPayType("WEIXIN_HIPO");
                // }
            }else if ("ALI_HIPO" == data.pay_type){
                window.location.href = data.return_data.pay_url;
            } else if("QQQB" == data.pay_type){
                if(submitQQQBPay){
                    submitQQQBPay(data.token_id,paySuccess,payCancel,payFail);
                }else{
                    forbiddenPayType("QQQB");
                }
            }else if("WEIXIN_H5" == data.pay_type){
                if(submitWeixinH5){
                    submitWeixinH5(data.mweb_url);
                }else{
                    forbiddenPayType("WEIXIN_H5");
                }
            }else if("PREPAIDCARDCODE" == data.pay_type){
                ljkPay(data.totalFee,data.phone);
            }else if("CMBBANKPAY" == data.pay_type){
                if(submitCmbBankPay){
                    submitCmbBankPay(data.return_from_text);
                }else{
                    forbiddenPayType("CMBBANKPAY");
                }
            }else if("H5_ABCBANKPAY" == data.pay_type){
                if(submitABCBankPay){
                    submitABCBankPay(data.return_from_text);
                }else{
                    forbiddenPayType("H5_ABCBANKPAY");
                }
            }
            else{
                myPub.TipLayer("不存在的支付方式");
            }
        });
    }
    exports.submitPay = submitPay;
    function paySuccess(){

        if(tempOrderType!= null && (tempOrderType == "GIFT"||tempOrderType == "MULTIGIFT")){
            myPub.locationHref("/shop/gift/sharePage/" + tempPaymentId);
            return;
        }
        var isGroupBuy = $orderForm.find('input[name="isGroupBuy"]').val();
        if("yes" == isGroupBuy){
            G_JSON_POST("/GroupBuying/getOrderInfo","orderSn="+tempOrderSn,function (data) {
                var recordId= data.recordId
                if(typeof recordId != 'undefined') {
                    // window.location.href = "/GroupBuying/GroupBuyingDetail/" + recordId;
                    myPub.locationHref("/GroupBuying/GroupBuyingDetail/" + recordId);
                }
            })
        } else {
            var _orderSn_kcuid = '';
            if(kcuid){
                // window.location.href = "/member/order/tradeSuccess?orderSn=" + tempOrderSn+"&kcuid="+kcuid;
                _orderSn_kcuid = "?orderSn=" + tempOrderSn + "&payId=" + tempPaymentId + "&kcuid=" + kcuid;

            }
            else {
                _orderSn_kcuid = "?orderSn=" + tempOrderSn + "&payId=" + tempPaymentId;
                // myPub.locationHref("/member/order/tradeSuccess?orderSn=" + tempOrderSn);
            }
            // myPub.locationHref("/member/order/tradeSuccess" + _orderSn_kcuid);
            myPub.locationHref("/h/goodspay/paymentOver" + _orderSn_kcuid);

        }

        // G_JSON_POST("/shop/payment/paymentSuccess",{'paymentId':paymentId},function(data){
        //     if("SUCCESS" == data.status){
        //         window.location.href = "javascript:void(0);";
        //     }else{
        //         alert(data.message);
        //     }
        // });

    }

    function payFail() {
        // TipLayer(res.errMsg);
        localStorage.setItem("orderNav_index",1);
        window.location.href = "/shop/pay/order_list";
    }

    function payCancel(){
        // TipLayer(res.errMsg);
        localStorage.setItem("orderNav_index",1);
        window.location.href = "/shop/pay/order_list";
    }

    function forbiddenPayType(type){
        myPub.TipLayerLong("暂不支持更换支付方式,请从订单下单渠道继续支付或重新下单");
    }


    function ljkPay(totalFee,phone) {
        $('.sxcashCardArea_sum').text("￥"+totalFee);
        $('#gfcUserPhone').val(phone)
        // $('#paymentId').val(paymentId)
        // $('#tempOrderSn').val(tempOrderSn);
        // $('#tempOrderType').val(tempOrderType);
        // lpkPay.ljkPsdShow();
        ljkPsdShow();

        myPub.HamstopPropaga();
    }

    function ljkPsdShow() {
        oCashMain.show().stop().animate({ 'bottom':0 },300,function() {
            oShadows.show();
        });
        myPub.HamstopPropaga();
    }
});